home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / btshowmetainfo.bittorrent < prev    next >
Encoding:
Text File  |  2007-02-19  |  1.8 KB  |  57 lines

  1. #! /usr/bin/python
  2.  
  3. # Written by Henry 'Pi' James and Loring Holden
  4. # see LICENSE.txt for license information
  5.  
  6. from sys import *
  7. from os.path import *
  8. from sha import *
  9. from BitTorrent.bencode import *
  10.  
  11. NAME, EXT = splitext(basename(argv[0]))
  12. VERSION = '20021207'
  13.  
  14. print '%s %s - decode BitTorrent metainfo files' % (NAME, VERSION)
  15. print
  16.  
  17. if len(argv) == 1:
  18.     print '%s file1.torrent file2.torrent file3.torrent ...' % argv[0]
  19.     print
  20.     exit(2) # common exit code for syntax error
  21.  
  22. for metainfo_name in argv[1:]:
  23.     metainfo_file = open(metainfo_name, 'rb')
  24.     metainfo = bdecode(metainfo_file.read())
  25.     metainfo_file.close()
  26.     announce = metainfo['announce']
  27.     info = metainfo['info']
  28.     info_hash = sha(bencode(info))
  29.  
  30.     print 'metainfo file.: %s' % basename(metainfo_name)
  31.     print 'info hash.....: %s' % info_hash.hexdigest()
  32.     piece_length = info['piece length']
  33.     if info.has_key('length'):
  34.         # let's assume we just have a file
  35.         print 'file name.....: %s' % info['name']
  36.         file_length = info['length']
  37.         name ='file size.....:'
  38.     else:
  39.         # let's assume we have a directory structure
  40.         print 'directory name: %s' % info['name']
  41.         print 'files.........: '
  42.         file_length = 0;
  43.         for file in info['files']:
  44.             path = ''
  45.             for item in file['path']:
  46.                 if (path != ''):
  47.                    path = path + "/"
  48.                 path = path + item
  49.             print '   %s (%d)' % (path, file['length'])
  50.             file_length += file['length']
  51.         name = 'archive size..:'
  52.     piece_number, last_piece_length = divmod(file_length, piece_length)
  53.     print '%s %i (%i * %i + %i)' \
  54.           % (name,file_length, piece_number, piece_length, last_piece_length)
  55.     print 'announce url..: %s' % announce
  56.     print
  57.